package main.servlet.service.impl;

import java.text.ParseException;
import java.util.Date;

import main.dao.UserDao;
import main.dao.impl.UserDaoImpl;
import main.pojo.User;
import main.servlet.service.VerifyService;
import main.servlet.service.listener.VerifyListener;
import main.util.ParseUtil;

public class VerifyServiceImpl implements VerifyService {

	private VerifyListener mListener;
	private User user;
	private boolean isUNameOk = false;
	private boolean isSalOk = false;
	private boolean isBirthOk = false;
	private boolean isSexOk = false;
	private boolean isHeadPicOk = false;
	private UserDao userDao;
	
	public VerifyServiceImpl(VerifyListener listener) {
		this.mListener = listener;
		this.user = new User();
		this.userDao = new UserDaoImpl();
	}

	public void setUserHeadPic(byte[] headPic){
		this.user.setHeadPic(headPic);
		this.isHeadPicOk = true;
		showVerifyResult();
	}
	
	@Override
	public boolean verifyUserName(String uname) {
		// /uname空值处理
		if (ParseUtil.getInstance().isNull(uname)) {
			if (this.mListener != null) {
				this.mListener.errorDuringVerify("用户名不能为空！");
				this.isUNameOk = false;
			}
			return false;
		}
		// /DB中用户名重复处理
		User u = this.userDao.searchUser(uname);
		if(u!=null){
			if (this.mListener != null) {
				this.mListener.errorDuringVerify("用户已注册！");
				this.isUNameOk = false;
			}
			return false;
		}

		this.user.setUname(uname.trim());
		this.isUNameOk = true;
		showVerifyResult();
		return true;
	}

	@Override
	public boolean verifySalary(String sal) {
		if(sal==null || sal.trim().length()==0){
			this.isSalOk = true;
			showVerifyResult();
			return true;
		}
		try {
			Double resSal = ParseUtil.getInstance().string2Double(sal);
			this.user.setSal(resSal);
			this.isSalOk = true;
			showVerifyResult();
		} catch (NumberFormatException e) {
			if (this.mListener != null) {
				this.mListener.errorDuringVerify("生日填写格式有误，正确格式：yyyy-MM-dd");
			}
			this.isSalOk = false;
			return false;
		}
		return true;
	}

	@Override
	public boolean verifyBirth(String birth) {
		if(birth==null || birth.trim().length()==0){
			this.isBirthOk = true;
			showVerifyResult();
			return true;
		}
		Date resBirth;
		try {
			resBirth = ParseUtil.getInstance().string2Date(birth);
			this.user.setBirth(resBirth);
			this.isBirthOk = true;
			showVerifyResult();
			return true;
		} catch (ParseException e) {
			if (this.mListener != null) {
				this.mListener.errorDuringVerify("生日填写格式有误，正确格式：yyyy-MM-dd");
			}
			return false;
		}

	}

	@Override
	public boolean verifySex(String sex) {
		if(sex==null || sex.trim().length()==0){
			this.isSexOk = true;
			showVerifyResult();
			return true;
		}
		this.user.setSex(ParseUtil.getInstance().string2Boolean(sex));
		this.isSexOk = true;
		showVerifyResult();
		return true;
	}

	private void showVerifyResult() {
		if (this.isHeadPicOk && this.isUNameOk&&this.isBirthOk&&this.isSalOk&&this.isSexOk && this.mListener != null) {
			this.mListener.successVerify(this.user);
		}
	}

}
